<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'connection.php';


class dao_choice
{

    //Methods
    public function __construct()
    {

    }

    public function get_all()
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT * " . "FROM tbl_choice WHERE game_id = 0";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function get_all_by_game_id($game_id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT * " . "FROM tbl_choice " . "WHERE game_id = '" . $game_id . "' ";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function get_all_by_status_id($status_id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT ms.name AS status_name, m.* " . "FROM tbl_choice m LEFT JOIN tbl_choice_status ms ON m.status_id = ms.id " . "WHERE ms.id = " . $status_id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }

    public function get_by_id($id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT * " . "FROM tbl_choice " . "WHERE id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();

        return $row;
    }

    public function get_by_name($name)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT * " . "FROM tbl_choice " . "WHERE name = '" . $name . "'";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect();

        return $row;
    }

    public function delete($id)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_choice WHERE id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect();

        return TRUE;
    }

    public function check_exist($id, $name)
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_choice WHERE id != $id AND name = '$name'";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());
        $row = mysqli_fetch_array($result);

        if ($row != NULL) {
            $db->close_connect();

            return TRUE;
        } else {
            $db->close_connect();

            return FALSE;
        }
    }

    public function  save($id, $name)
    {
        $db  = new connection();
        $con = $db->open_connect();

        if ($this->check_exist($id, $name)) {
            return FALSE;
        } else if ($id == 0) {
            $query = "INSERT INTO tbl_choice(name, game_id) VALUES ('$name', 0)";
        } else {
            $query = "UPDATE tbl_choice SET name = '$name' WHERE id = $id";
        }

        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $inserted_id = TRUE;
        if ($id == 0) {
            $inserted_id = mysqli_insert_id($db->con);
        }
        $db->close_connect();

        return $inserted_id;
    }

    public function get_all_roulette_choice()
    {
        $db  = new connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_choice WHERE game_id != 0";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }
        $db->close_connect();

        return $list;
    }
}
